package com.dx.financehy.serializer;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 手机号码脱敏序列化器
 * @JsonSerialize(using = PhoneHideSerializer.class)
 */
public class PhoneHideSerializer extends JsonSerializer<String> {
    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (StrUtil.length(s) == 11) {
            jsonGenerator.writeString(IdcardUtil.hide(s, 3, 8));
            return;
        }
        jsonGenerator.writeString(s);
    }
}
